#include <unistd.h>
// #include "room.hpp"
#include "session.hpp"
#include "matcher.hpp"
#include "server.hpp"

#define HOST "127.0.0.1"
#define PORT 3306
#define DATABASE "gobang"
#define USER "root"
#define PASSWORD "123456"


void logger_test(){
    ILOG("%s:%d", "weihe", 1024);
    DLOG("%s:%d", "weihe", 1024);
    ELOG("%s:%d", "weihe", 1024);
    // LOG("weihe");
}
void mysql_test(){
    MYSQL *mysql = mysql_util::mysql_create(HOST, USER, PASSWORD, DATABASE, PORT);
    const char *sql = "insert stu values(null, '未禾', 18, 60, 60, 60);";
    bool ret = mysql_util::mysql_exec(mysql, sql);
    if(ret == false){
        return;
    }
    mysql_util::mysql_destroy(mysql);
}
void json_test(){
    // 1. 创建一个Value对象
    Json::Value root;
    std::string str;
    root["name"] = "weihe";
    root["age"] = 18;
    root["score"].append(80);
    root["score"].append(85.4);
    root["score"].append(65.1);
    json_util::serialize(root, str);
    DLOG("%s", str.c_str());

    Json::Value val;
    json_util::unserialize(str, val);
    std::cout << "name: " << val["name"].asString() << std::endl;
    std::cout << "age: " << val["age"].asInt() << std::endl;
    for(int i = 0; i < val["score"].size(); i++){
        std::cout << "score " << i << ": " << val["score"][i].asInt() << std::endl;
    }
}
void string_test(){
    std::string src = ",,....,,,weihe,1024,,,2323,,,,";
    std::vector<std::string> dest;
    string_util::split(src, ",", dest);
    for(const auto &e : dest){
        DLOG("%s", e.c_str());
    }
}
void file_test(){
    std::string body;
    file_util::read("./logger.hpp", body);
    DLOG("%s", body.c_str());
}
void db_test(){
    user_table ut(HOST, USER, PASSWORD, DATABASE, PORT);
    Json::Value user;
    user["username"] = "weihe213";
    user["password"] = "12345sad";
    ut.insert(user);
    // bool ret = ut.login(user);
    // if (ret == false){
    //     DLOG("login failed!");
    //     return;
    // }
    // std::string body;
    // ut.select_by_name("weih", user);
    // ut.select_by_id(10, user);
    // json_util::serialize(user, body);
    // DLOG("%s", body.c_str());

    // sleep(5);
    // ut.win(1);
    // ut.win(1);
    // ut.win(1);

    // sleep(10);
    // ut.lose(1);
    // ut.lose(1);
    // ut.lose(1);

}
void online_manager_test(){
    online_manager om;
    wsserver_t::connection_ptr conn;
    uint64_t uid = 2;
    om.enter_game_room(uid, conn);
    if(om.is_in_game_room(uid)) {
        DLOG("in game room!");
    }
    else {
        DLOG("not in room!");
    }
    om.exit_game_room(uid);
    if(om.is_in_game_room(uid)) {
        DLOG("in game room!");
    }
    else {
        DLOG("not in room!");
    }
}
void room_test() {
    user_table ut(HOST, USER, PASSWORD, DATABASE, PORT);
    online_manager om;
    room r(1, &ut, &om);
}
void room_manager_test() {
    user_table ut(HOST, USER, PASSWORD, DATABASE, PORT);
    online_manager om;
    room_manager rm(&ut, &om);
    room_ptr rp = rm.create_room(1, 2);
}
void session_manager_test() {
    wsserver_t srv;
    session_manager ssm(&srv);
    session_ptr ssp = ssm.create_session(1, LOGIN);
    ssm.set_session_expire_time(ssp->get_ssid(), 1000);
    // ssm.remove_session(ssp->get_ssid());
}
void matcher_test() {
    user_table ut(HOST, USER, PASSWORD, DATABASE, PORT);
    online_manager om;
    room_manager rm(&ut, &om);
    matcher m(&rm, &ut, &om);
}

void server_test() {
    gobang_server srv(HOST, USER, PASSWORD, DATABASE, PORT);
    srv.start(8085);
}
int main(){
    server_test();
    
    return 0;
}
